package com.rc.evcharger.common.terminal;

import com.rc.evcharger.common.annotation.ByteLength;
import com.rc.evcharger.common.enums.ElecPileStopReasonEnum;
import com.rc.evcharger.common.utils.DateHelper;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 告警码接受类
 */
public class FaultCodeResult implements Result {

    @ByteLength(length = 1)
    Integer number;//告警数量

    @ByteLength(length = 1)
    String gunNum; // 枪号

    @ByteLength(length = 6)
    Date addTime;     //添加时间

    @ByteLength(length = 1)
    List<String> code = new ArrayList<>(); //告警编码;

    public Integer getNumber() {
        return number;
    }

    public void setNumber(Integer number) {
        this.number = number;
    }

    public String getGunNum() {
        return gunNum;
    }

    public void setGunNum(String gunNum) {
        this.gunNum = gunNum;
    }

    public Date getAddTime() {
        return addTime;
    }

    public void setAddTime(Date addTime) {
        this.addTime = addTime;
    }

    public List<String> getCode() {
        return code;
    }

    public void setCode(List<String> code) {
        this.code = code;
    }

    @Override
    public List<FaultCodeResult> getResult(String dataStr) {
        List<FaultCodeResult> result = new ArrayList<>();
        FaultCodeResult fcr = new FaultCodeResult();
        StringBuilder sb = new StringBuilder("20");
        String data = dataStr.trim();
        // 获取告警数量;
        fcr.number = Integer.valueOf(data.substring(0, 2));
        data = data.substring(3);
        // 获取测量点号;
        fcr.gunNum = data.substring(0, 2);
        data = data.substring(3);
        //年
        sb.append(data.substring(0, 2));
        data = data.substring(3);
        //月
        sb.append("-").append(data.substring(0, 2));
        data = data.substring(3);
        //日
        sb.append("-").append(data.substring(0, 2));
        data = data.substring(3);
        //时
        sb.append(" ").append(data.substring(0, 2));
        data = data.substring(3);
        //分
        sb.append(":").append(data.substring(0, 2));
        data = data.substring(3);
        //秒
        sb.append(":").append(data.substring(0, 2));
        data = data.substring(3);
        //添加时间
        fcr.addTime = DateHelper.stringToDate(sb.toString(), DateHelper.DATE_COMMON_FORMAT);
        //告警编码
        while (data.length() >= 2) {
            fcr.code.add(data.substring(0, 2));
            if (data.length() == 2) {
                data = data.substring(2);
            } else {
                data = data.substring(3);
            }

        }
        result.add(fcr);
        return result;
    }

    public static void main(String[] args) {
        String aa = "01 01 20 04 18 17 10 00 00 98 16 4B 00";
        List<FaultCodeResult> result = new FaultCodeResult().getResult(aa);
        String fault = "";
        for (FaultCodeResult faultCodeResult : result) {
            for (String s : faultCodeResult.getCode()) {
                //获取电桩故障
                fault += ElecPileStopReasonEnum.getElecPileStopReasonResult(s) + ",";
            }
        }
        System.out.println(fault);
    }
}
